package com.mango.ch11;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import org.apache.hadoop.io.WritableComparable;

public class CompositeKey implements WritableComparable<CompositeKey> {

    private String name;
    private Long date;

    @Override
    public String toString() {
        return name + "\t" + date;
    }

    public CompositeKey() {
    }

    public CompositeKey(String name, Long date) {
        super();
        this.name = name;
        this.date = date;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Long getDate() {
        return date;
    }

    public void setDate(Long date) {
        this.date = date;
    }

    @Override
    public void write(DataOutput out) throws IOException {
        // TODO Auto-generated method stub
        out.writeUTF(name);
        out.writeLong(date);
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        // TODO Auto-generated method stub
        this.name = in.readUTF();
        this.date = in.readLong();
    }

    @Override
    public int compareTo(CompositeKey ot) {
        // TODO Auto-generated method stub
        int com = this.name.compareTo(ot.getName());
        if (com == 0) {
            if (this.date != ot.getDate())
                return (this.date > ot.getDate()) ? 1 : -1;
            else
                return 0;
        }
        return com;
    }

}
